package com.smooth.site.sx.pc.job.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smooth.dao.gen.BizJobApplayMapper;
import com.smooth.dao.gen.BizJobMapper;
import com.smooth.dao.gen.BizStudentMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.dao.gen.bean.BizJobApplayExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.dao.gen.bean.BizStudentExample;
import com.smooth.site.sx.mag.basedata.college.service.BaseCollegeService;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.PcCollegeCompService;
import com.smooth.site.sx.mag.biz.student.service.BizStudentService;
import com.smooth.site.sx.pc.job.enums.ApplyJobStatus;
import com.smooth.site.sx.pc.job.enums.ApplyJobType;
import com.smooth.site.sx.pc.job.req.invite.InviteApplyReq;
import com.smooth.sys.pc.vo.ListRsp;

@Component
public class PcInviteApplyService {
	Logger log = LoggerFactory.getLogger(PcInviteApplyService.class);
	@Autowired
	BizStudentService bizStudentService;
	@Autowired
	PcCollegeCompService pcCollegeCompService;
	@Autowired
	BizJobMapper bizJobMapper;
	@Autowired
	BizJobApplayMapper bizJobApplayMapper;
	@Autowired
	BizJobApplayService bizJobApplayService;
	public ListRsp<BizJobApplay> queryStuJobs(InviteApplyReq inviteApplyReq) {
		ListRsp<BizJobApplay> brsp= new ListRsp<BizJobApplay>();
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		if(inviteApplyReq.getApplyJobStatus()!=null) {
			if(ApplyJobStatus.applysuccess.value.equals(inviteApplyReq.getApplyJobStatus())) {
				List<Integer> ilist = new ArrayList<Integer>();
				ilist.add(ApplyJobStatus.applysuccess.value);
				ilist.add(ApplyJobStatus.offerCreated.value);
				criteria.andApplayStatusIn(ilist);
			}else {
				criteria.andApplayStatusEqualTo(inviteApplyReq.getApplyJobStatus());
			}
		}
		criteria.andStuIdEqualTo(inviteApplyReq.getUid());
		criteria.andApplyTypeEqualTo(ApplyJobType.yaoqinghan.value);
		bizJobApplayExample.setOrderByClause(" create_date desc ");
		List<BizJobApplay>  blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobApplayMapper.countByExample(bizJobApplayExample));
		
		return brsp;
	}
	
	public BizJobApplay queryApplyByUidJobid(String stuId,String jobId) {
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		criteria.andJobIdEqualTo(stuId);
		criteria.andStuIdEqualTo(jobId);
		criteria.andApplyTypeEqualTo(ApplyJobType.yaoqinghan.value);
		List<BizJobApplay> blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	
	
	public BizJobApplay queryApplyByUidCompid(String stuId,String compId) {
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		criteria.andStuIdEqualTo(stuId);
		criteria.andCompIdEqualTo(compId);
		criteria.andApplyTypeEqualTo(ApplyJobType.yaoqinghan.value);
		List<BizJobApplay> blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	
	@Autowired
	BizStudentMapper bizStudentMapper;
	//公司
	public List<BizStudent> queryCompStudents(String compId){
		List<String> collegeIdList = pcCollegeCompService.querCollegeByComp(compId);
		if(collegeIdList.size()>0) {
			BizStudentExample bizStudentExample = new BizStudentExample();
			BizStudentExample.Criteria criteria = bizStudentExample.createCriteria();
			
			List<String> stuIdList = getCompSendApplyIds(compId);
			criteria.andSchIdIn(collegeIdList);
			if(stuIdList.size()>0) {
				criteria.andIdNotIn(stuIdList);
			}
			List<BizStudent> blist = bizStudentMapper.selectByExample(bizStudentExample);
			return blist;
		}
		return new ArrayList<BizStudent>();
	}
	public List<String> getCompSendApplyIds(String compId){
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		criteria.andCompIdEqualTo(compId);
		criteria.andApplyTypeEqualTo(ApplyJobType.yaoqinghan.value);
		bizJobApplayExample.setOrderByClause(" create_date desc ");
		List<BizJobApplay>  blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		
		List<String> alist = new ArrayList<String>();
		for(BizJobApplay bizJobApplay : blist) {
			alist.add(bizJobApplay.getStuId());
		}
		return alist;
	}
	@Autowired
	BaseCollegeService baseCollegeService;
	public List<BizJobApplay> sendApplys(String[] studIds,BizCompany bizCompany){
		List<BizJobApplay>  blist = new ArrayList<>();
		Date date = new Date();
		for(String std:studIds) {
			if(StringUtils.isNotBlank(std)) {
				if(queryApplyByUidCompid(std,bizCompany.getId())==null) {
					BizJobApplay bizJobApplay = new BizJobApplay();
					bizJobApplay.setApplayStatus(ApplyJobStatus.compsend.value);
					bizJobApplay.setCompId(bizCompany.getId());
					bizJobApplay.setCompName(bizCompany.getCompName());
					bizJobApplay.setCreateDate(date);
//					bizJobApplay.setJobId(jobId);
//					bizJobApplay.setJobTitle(jobTitle);
					bizJobApplay.setStuId(std);
					BizStudent bizStudent = bizStudentService.get(std);
					bizJobApplay.setStuName(bizStudent.getStuName());
					bizJobApplay.setStuTelNo(bizStudent.getTelNo());
					bizJobApplay.setUpdateDate(date);
					bizJobApplay.setApplyType(ApplyJobType.yaoqinghan.value);
					bizJobApplay.setCollegeId(bizStudent.getSchId());
					bizJobApplay.setCollegeName(bizStudent.getSchName());
					bizJobApplayService.save(bizJobApplay);
				}else {
					log.info(std+" 公司："+bizCompany.getId()+" 已经存在 直接跳过");
				}
				
			}
		}
		return blist;
	}
	
	public ListRsp<BizJobApplay> queryCompJobs(InviteApplyReq inviteApplyReq) {
		ListRsp<BizJobApplay> brsp= new ListRsp<BizJobApplay>();
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		if(inviteApplyReq.getApplyJobStatus()!=null) {
			ApplyJobStatus applyJobStatus = ApplyJobStatus.getStatus(inviteApplyReq.getApplyJobStatus());
			if(ApplyJobStatus.applysuccess.equals(applyJobStatus)) {
				List<Integer> ilist = new ArrayList<Integer>();
				ilist.add(ApplyJobStatus.applysuccess.value);
				ilist.add(ApplyJobStatus.offerCreated.value);
				criteria.andApplayStatusIn(ilist);
			}else {
				criteria.andApplayStatusEqualTo(inviteApplyReq.getApplyJobStatus());
			}
		}
		criteria.andCompIdEqualTo(inviteApplyReq.getUid());
		criteria.andApplyTypeEqualTo(ApplyJobType.yaoqinghan.value);
		bizJobApplayExample.setOrderByClause(" create_date desc ");
		List<BizJobApplay>  blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobApplayMapper.countByExample(bizJobApplayExample));
		
		return brsp;
	}
}
